/**
 * 文件：EditCardTextBox.java
 * 包名：com.ronri.list.editview.controls;   --- 正式发布时需要修正
 * 说明：身份证输入
 * 作者：MS During
 * 日期：2017/4/14
 * 历史：
 */
package com.ronri.list.editview.controls;

import android.content.Context;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.application.InitApplication;
import com.ronri.common.SystemManager;



public class EditCardTextBox extends EditTextBox {

    // 值
    private String mValidate= "";

    // 弹框
    PopupWindow popupWindow = null;

    /***
     * 构造函数
     */
    public EditCardTextBox() {
        super();
        this.ControlName = "EditCardTextBox";
        this.BindLayout(R.layout.control_default_textbox);
    }

    /***
     * 装载控件
     * @return
     */
    @Override
    public boolean LoadEditControl() {
        return super.LoadEditControl();
    }

    /***
     * 显示控件
     * @return
     */
    @Override
    public boolean ShowEditControl(View Anchor) {

        WindowManager manager = (WindowManager) InitApplication.getInstance().getSystemService(Context.WINDOW_SERVICE);
        @SuppressWarnings("deprecation")
        int nWidth = manager.getDefaultDisplay().getWidth();
        int nHeight = manager.getDefaultDisplay().getHeight();

        View view = getView();

        ImageView imgViewCancel = (ImageView) view.findViewById(R.id.edit_fail);
        TextView imgViewOK = (TextView)view.findViewById(R.id.edit_success);
        TextView textView = (TextView)view.findViewById(R.id.controlEdit);

        Button btVoice = (Button)view.findViewById(R.id.buttonGetVoice);
        btVoice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getVoiceText();
            }
        });


        imgViewCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });


        /***
         * 确认返回结果集
         */
        imgViewOK.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(BaseControlEevnt != null){

                    // 校验数据
                    boolean bRet = Validate();

                    // 回调，传递结果
                    BaseControlEevnt.ReturnValue(this);
                }

                popupWindow.dismiss();
            }
        });


        popupWindow = new PopupWindow(view,
                WindowManager.LayoutParams.MATCH_PARENT,
                nHeight - SystemManager.getStatusBarHeight(), true);

        textView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {

                if(event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_BACK)
                    popupWindow.dismiss();

                return false;
            }
        });

        // 设置弹出窗体显示时的动画，从底部向上弹出
        popupWindow.setAnimationStyle(R.style.take_photo_anim);
        popupWindow.showAtLocation(Anchor, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);

        return super.ShowEditControl(Anchor);
    }

    /***
     * 隐藏控件
     * @return
     */
    @Override
    public boolean HideEditControl() {
        return super.HideEditControl();
    }

    /****
     * 身份证验证
     * @return
     */
    @Override
    public boolean Validate() {

        EditText ctl = (EditText)popupWindow.getContentView().findViewById(R.id.controlEdit);
        String szValue = ctl.getText().toString();

        if (szValue.length() != 13) {
            return false;
        }

        return false;
    }

    @Override
    public String toString() {
        return "adfasdfasd";
    }

    /***
     * 获取错误信息
     * @return
     */
    @Override
    public String GetErrorMessage() {
        return getContext().getString(R.string.validate_card_error);
    }

    @Override
    public void CallBackResult(String szString) {
        super.CallBackResult(szString);
        EditText editText = (EditText) popupWindow.getContentView().findViewById(R.id.controlEdit);
        editText.setText(szString);
    }
}
